var global = {
  resizeFunc: []
};

function registerResize(func) {
  global.resizeFunc.push(func);
}

function onResize() {
  var $win = $(window);
  global.winWidth = global.W = $win.width();
  global.winHeight = global.H = $win.height();

  _.each(global.resizeFunc, function(func) {
    try {
      func.call();
    } catch (e) {}
  });
}

function scrollable(selector) {
  $(selector).jScrollPane();
}


function switchContent(toggleSelector, relatedSelector) {
  $(toggleSelector).click(function() {
    $(toggleSelector).filter('.active').removeClass('active');
    var $this = $(this);
    var ref = $this.data('ref');
    $this.addClass('active');
    $(relatedSelector).filter('.active').removeClass('active').css({
      display: 'none'
    });
    $('#' + ref).fadeIn().addClass('active').jScrollPane();
  });
}

var mousNow = 0;
var mousBool = false;
$(window).mousewheel(function(event, delta, deltaX, deltaY) {
  event.preventDefault();

  if (mousBool) {
    return;
  }
  var he = global.H;
  var len = $(".page-section").length;

  if (delta > 0) {
    if (mousNow - 1 < 0) {
      return;
    } else
      mousNow--;
  } else {
    if (mousNow + 1 > len - 1) {
      return;
    } else
      mousNow++;
  }
  mousAn(mousNow);
})


function mousAn(mousNow) {
  var he = global.H;

  if (he * mousNow != $(window).scrollTop() && !mousBool) {
    mousBool = true;
    $("body,html").stop(true, false).animate({
      scrollTop: he * mousNow
    }, 500, function() {
      setTimeout(function(){mousBool = false;},500);
    });
  }
};


$(function() {
  onResize();
  $(window).resize(onResize);
});